﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StandardManager.EntityCode
{
    public partial class EntityFullCodeForm : Form
    {
        public EntityFullCodeForm()
        {
            InitializeComponent();
        }

        private void EntityFullCodeForm_Load(object sender, EventArgs e)
        {
            var rs = new DirectoryInfo(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "Resources"));
            var fs = rs.GetFiles("*.json");
            for (var i = 0; i < fs.Length; i++)
            {
                var jsonFile = fs[i].FullName;

                var imgFile = Path.ChangeExtension(jsonFile, ".png");
                if (File.Exists(imgFile) == false)
                {
                    imgFile = Path.ChangeExtension(jsonFile, ".jpg");
                }
                if (File.Exists(imgFile) == false)
                {
                    imgFile = null;
                }
                var jdata = Newtonsoft.Json.JsonConvert.DeserializeObject(File.ReadAllText(jsonFile)) as JToken;
                var tName = jdata["title"].ToString();
                this.listBox1.Items.Add(tName);
                var tablePanel = new TabPage(tName);
                var splitPanel = new SplitContainer();
                splitPanel.Dock = DockStyle.Fill;
                splitPanel.SplitterDistance = (int)(this.Width * 0.6); ;
                tablePanel.Controls.Add(splitPanel);
                if (imgFile != null)
                    splitPanel.Panel1.BackgroundImage = Image.FromFile(imgFile);
                splitPanel.Panel1.BackgroundImageLayout = ImageLayout.Stretch;

                TreeView treeView = new TreeView();
                treeView.Dock = DockStyle.Fill;
                treeView.Font = new Font("微软雅黑", 11);
                for (var j = 0; j < jdata.Count(); j++)
                {
                    var keyValue = jdata.ElementAt(j) as JToken;

                    TreeNode treeNode = new TreeNode(((Newtonsoft.Json.Linq.JProperty)keyValue).Name);

                    ParseChildNode(treeNode, keyValue);

                    treeView.Nodes.Add(treeNode);
                }
                splitPanel.Panel2.Controls.Add(treeView);

                this.tabControl1.TabPages.Add(tablePanel);
            }
        }

        private void ParseChildNode(TreeNode treeNode, JToken token)
        {
            if (token is JObject)
            {
                var jobject = token as JObject;
                var jobjectChildren = jobject.Children();
                for (var i = 0; i < jobjectChildren.Count(); i++)
                {
                    var jobj = jobjectChildren.ElementAt(i) as JToken;
                    TreeNode ctreeNode = new TreeNode(((Newtonsoft.Json.Linq.JProperty)jobj).Name);
                    treeNode.Nodes.Add(ctreeNode);
                    ParseChildNode(ctreeNode, jobj);
                }
            }
            else if (token is Newtonsoft.Json.Linq.JValue)
            {
                Newtonsoft.Json.Linq.JValue jvalueToken = token as Newtonsoft.Json.Linq.JValue;
                TreeNode ctreeNode = new TreeNode(jvalueToken.Value.ToString());
                treeNode.Nodes.Add(ctreeNode);
            }
            else
            {
                var value = ((Newtonsoft.Json.Linq.JProperty)token).Value;
                if (value is Newtonsoft.Json.Linq.JArray)
                {
                    var jvalue = value as Newtonsoft.Json.Linq.JArray;
                    for (var i = 0; i < jvalue.Count(); i++)
                    {
                        var jt3 = jvalue.ElementAt(i);
                        ParseChildNode(treeNode, jt3);
                    }
                }
                else
                {
                    var v2 = ((Newtonsoft.Json.Linq.JValue)value).Value;
                    TreeNode tn = new TreeNode(v2.ToString());
                    treeNode.Nodes.Add(tn);
                }

            }
        }

        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.listBox1.SelectedItems.Count == 0) { return; }
            var listViewItem = this.listBox1.SelectedItems[0];
            for(var i=0; i<this.tabControl1.TabPages.Count; i++)
            {
                if (this.tabControl1.TabPages[i].Text== listViewItem.ToString())
                {
                    this.tabControl1.SelectedTab = this.tabControl1.TabPages[i];
                }
            }
        }
    }
}
